#define  _CRT_SECURE_NO_WARNINGS 1
typedef struct ListNode ListNode;

ListNode* newnode(int x)
{
    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
    if (newnode == NULL)
    {
        exit(1);
    }
    newnode->val = x;
    newnode->next = NULL;
    return newnode;

}

ListNode* CreateNode(int n)
{
    ListNode* ptail = newnode(1);
    ListNode* pcur = ptail;
    for (int i = 2; i <= n; i++)
    {
        pcur->next = newnode(i);
        pcur = pcur->next;
    }
    pcur->next = ptail;
    return pcur;
}

int ysf(int n, int m)
{
    ListNode* prev = CreateNode(n);
    ListNode* pcur = prev->next;
    int count = 1;
    while (pcur != pcur->next)
    {
        if (count == m)
        {
            prev->next = pcur->next;
            free(pcur);
            pcur = prev->next;
            count = 1;
        }
        else
        {
            prev = pcur;
            pcur = prev->next;
            count++;
        }

    }
    int data = pcur->val;
    free(pcur);
    pcur = NULL;
    prev = NULL;


    return data;

}